package com.liuyu.common.framework.feignclient.api.feign.interceptor;

import cn.hutool.core.util.IdUtil;
import com.liuyu.common.framework.conf.PowerProperties;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 集中权限认证feignclient 拦截器
 *
 * @author: liuyu
 * @date: 2023/1/4 11:12
 * @version: 1
 */
public class PowerAuthFeignInterceptor implements RequestInterceptor {
    private static final String VERSION = "version";
    private static final String APP_KEY = "appKey";
    private static final String TIMESTAMP = "timestamp";
    private static final String NONCE = "nonce";
    private static final String SIGN = "sign";
    private static final String VERSION_NO = "2";
    @Autowired
    private PowerProperties powerProperties;


    @Override
    public void apply(RequestTemplate requestTemplate) {
        //集中权限增加鉴权header
        requestTemplate.header(VERSION, VERSION_NO);
        requestTemplate.header(APP_KEY, powerProperties.getAppKey());
        String timestamp = String.valueOf(System.currentTimeMillis());
        requestTemplate.header(TIMESTAMP, timestamp);
        String nonce = IdUtil.getSnowflakeNextIdStr();
        requestTemplate.header(NONCE, nonce);
        String[] strArray = new String[]{powerProperties.getAppKey(), powerProperties.getAppSecret(), timestamp, nonce};
        String sign = PowerCloudSignUtil.sign(strArray);
        requestTemplate.header(SIGN, sign);
    }
}
